#include "hud_stats.h"
#include "../core/actor.h"
#include "../stats/stats.h"
#include "../affiliate/sprite.h"

void hud_stats::init()
{
    screen::init();
    hp_bar_bg = sprite::createSprite(this,"assets/UI/bar_bg.png",3.0f,ANCHOR_CENTER_LEFT);
    hp_bar_bg->set_offset(hp_bar_bg->get_offset() + glm::vec2(90.0f, 0.0f));
    hp_bar = sprite::createSprite(this,"assets/UI/bar_red.png",3.0f,ANCHOR_CENTER_LEFT);
    hp_bar->set_offset(hp_bar->get_offset() + glm::vec2(90.0f, 0.0f));
    hp_bar_icon = sprite::createSprite(this,"assets/UI/Red Potion.png",0.5f,ANCHOR_CENTER_LEFT);

    mp_bar_bg = sprite::createSprite(this,"assets/UI/bar_bg.png",3.0f,ANCHOR_CENTER_LEFT);
    mp_bar_bg->set_offset(mp_bar_bg->get_offset() + glm::vec2(310.0f, 0.0f));
    mp_bar = sprite::createSprite(this,"assets/UI/bar_blue.png",3.0f,ANCHOR_CENTER_LEFT);
    mp_bar->set_offset(mp_bar->get_offset() + glm::vec2(310.0f, 0.0f));
    mp_bar_icon = sprite::createSprite(this,"assets/UI/Blue Potion.png",0.5f,ANCHOR_CENTER_LEFT);
    mp_bar_icon->set_offset(mp_bar_icon->get_offset() + glm::vec2(220.0f, 0.0f));
}

void hud_stats::update(float dt)
{
    screen::update(dt);
    updateHpBar(dt);
    updateMpBar(dt);
}

hud_stats *hud_stats::createHubStats(node *parent, actor *target, glm::vec2 render_position)
{
    hud_stats *stats = new hud_stats();
    stats->init();
    stats->setTarget(target);
    stats->setRenderPosition(render_position);
    if(parent)parent->addChild(stats);
    return stats;
}

void hud_stats::updateHpBar(float dt)
{
    if(!target||!hp_bar||target->getStats() == nullptr)return;
    hp_bar->setPreScale({target->getStats()->getHp() / target->getStats()->getMaxHp(), 1.0f});
}

void hud_stats::updateMpBar(float dt)
{
    if(!target||!mp_bar||target->getStats() == nullptr)return;
    mp_bar->setPreScale({target->getStats()->getMana() / target->getStats()->getMaxMana(), 1.0f});
}